import React from 'react'
import PropTypes from 'prop-types'
import {routerRedux} from 'dva/router'
import {connect} from 'dva'
import {Col, Modal, Row, Card} from 'antd'
import {Page} from 'components'
import queryString from 'query-string'
import List from './components/List'
import Filter from './components/Filter'

const Chatroom = ({
                    location, dispatch, chatroom, loading,
                  }) => {
  const {query, pathname} = location
  const {
    list, pagination, stat, expandForm, currentItem, modalVisible, modalType, selectedRowKeys, empDataSource: dataSource,
  } = chatroom

  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery,
      }),
    }))
  }

  const ColProps = {
    xs: 24,
    sm: 12,
    style: {
      marginBottom: 16,
    },
  }

  const modalProps = {
    item: modalType === 'create' ? {} : currentItem,
    visible: modalVisible,
    width: 1600,
    maskClosable: false,
    confirmLoading: loading.effects[`chatroom/${modalType}`],
    title: '新增微信运营号',
    wrapClassName: 'vertical-center-modal',
    onOk (data) {
      dispatch({
        type: `chatroom/${modalType}`,
        payload: data,
      })
        .then(() => {
          handleRefresh()
        })
    },
    onCancel () {
      dispatch({
        type: 'chatroom/hideModal',
      })
    },
  }

  const listProps = {
    rowKey: 'id',
    dataSource: list,
    dispatch,
    loading: loading.effects['chatroom/query'],
    pagination,
    location,
    onChange (page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize,
      })
    },
    openTab (record) {
      const tab = {
        name: '群聊-'.concat(record.chatroomName || record.chatroomId),
        route: '/chatroom/'.concat(record.chatroomId),
      }
      dispatch({
        type: 'app/addTab',
        payload: {
          menu: tab,
        },
      })

      dispatch({
        type: 'chatroomDetail/getDetail',
        payload: {
          data: record,
        },
      })
    },
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'chatroom/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
    },
  }

  const filterProps = {
    dataSource,
    stat,
    total: pagination.total,
    handleSearch (key) {
      if (key) {
        dispatch({
          type: 'chatroom/searchEmp',
          payload: {
            key,
          },
        })
      }
    },
    filter: {
      ...query,
    },
    expandForm,
    toggleForm () {
      dispatch({type: 'chatroom/toggleForm'})
    },
    onFilterChange (value) {
      handleRefresh({
        ...value,
        current: 1,
      })
    },
  }

  return (
    <div>
      <Row gutter={24}>
        <Card style={{marginBottom: 12}}>
          <Filter {...filterProps} />
        </Card>
      </Row>
      <Row gutter={24}>
        <Page inner>
          <Row gutter={24}>
            <Col {...ColProps} xl={{span: 24}} md={{span: 24}} sm={{span: 24}}>
              <div style={{display: 'flex', justifyContent: 'space-between', flexWrap: 'wrap', float: 'right'}}>
              </div>
            </Col>
          </Row>
          <List {...listProps} />
          {modalVisible && <Modal {...modalProps} />}
        </Page>
      </Row>
    </div>
  )
}

Chatroom.propTypes = {
  chatroom: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({chatroom, loading}) => ({chatroom, loading}))(Chatroom)
